//
//  LoginViewCnntroller.m
//  哎游
//
//  Created by 林程杰 on 15/1/8.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LoginViewCnntroller.h"
#import "WebColor.h"
#import "MainViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "sinaWiboViewController.h"
#import "Masonry.h"
#import "WebColor.h"
#import "registerViewController.h"
#import "LocalViewController.h"
#import "UserDataBase.h"
#import "Singleton.h"

@implementation LoginViewCnntroller

- (void)dealloc
{
    [_imageBig release];
    [_scroll release];
    [_label release];
    [_page release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor clearColor]];
    self.navigationController.navigationBar.hidden = YES;
    [self creatView];
}

- (void) creatView
{
    
    self.imageBig = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _imageBig.image = [UIImage imageNamed:@"LaunchScreen.png"];
    _imageBig.userInteractionEnabled = YES;
    [self.view addSubview:_imageBig];
    
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height *3 / 4)];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height *3 / 4);
    _scroll.pagingEnabled = YES;
    _scroll.userInteractionEnabled = YES;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.showsVerticalScrollIndicator = NO;
    _scroll.delegate =self;
    //设置滚动图标
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 2 - 35, self.view.frame.size.height *2 / 3 + 20, 50, 30)];
    _page.numberOfPages = 2;
    
    if (_scroll.contentOffset.x == _scroll.frame.size.width) {
        _scroll.scrollEnabled = NO;
    }
    

    [self.view addSubview:_scroll];
    [self.view addSubview:_page];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 130, 95, 95)];
    _imageView.image = [UIImage imageNamed:@"big.png"];
    [_scroll addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_scroll.mas_left).offset(_scroll.frame.size.width / 2 - 50);
        make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height / 4);
        make.size.mas_equalTo(CGSizeMake(95, 95));
    }];
    
    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(appear:) userInfo:nil repeats:NO];// 1
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 2 - 80, _scroll.frame.size.height * 3 / 4, 400, 30)];
    [_label setText:@"一 起 旅 游  .  享 受 生 活"];
    _label.backgroundColor = [UIColor clearColor];
    _label.font = [UIFont systemFontOfSize:13];
    [_label setTextColor:[UIColor whiteColor]];
    _label.tag = 100;
    [_scroll addSubview:_label];
    //加入主标题
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 1.24, self.view.frame.size.height / 6.5, self.view.frame.size.width * 3 / 2, 150)];
    [labelTitle setText:@"哎 游"];
    labelTitle.backgroundColor = [UIColor clearColor];
    [labelTitle setTextColor:[UIColor whiteColor]];
    labelTitle.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:80];//设置字体样式
    [_scroll addSubview:labelTitle];
    
    UILabel *labelDetil = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width *1.24, self.view.frame.size.height / 2.5, self.view.frame.size.width *3 / 2, 30)];
    labelDetil.backgroundColor = [UIColor clearColor];
    [labelDetil setText:@"爱   •   就  一  起  旅  游  吧"];
    [labelDetil setTextColor:[UIColor whiteColor]];
    labelDetil.font = [UIFont systemFontOfSize:15];
    [_scroll addSubview:labelDetil];
    
   
    
    //返回按钮
    UIButton *buttonBack = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.7, self.view.frame.size.height - 30, 80, 20)];
  
    [buttonBack setTitle:@"随便看看" forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    buttonBack.titleLabel.font = [UIFont systemFontOfSize:13];
    [buttonBack setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    buttonBack.layer.cornerRadius = 13;
    buttonBack.clipsToBounds = YES;
      [buttonBack addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBack];
    
    //设置第三方button
    
    UIButton *buttonSina = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 8.7, self.view.frame.size.height *3.05 / 4, 64, 64)];
    
    buttonSina.layer.cornerRadius = 32;
    buttonSina.clipsToBounds = YES;
    [buttonSina setBackgroundImage:[UIImage  imageNamed:@"sinaicon.png"] forState:UIControlStateNormal];
    [buttonSina addTarget:self action:@selector(buttonSina:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonqq = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.5, self.view.frame.size.height *3.05 / 4, 64, 64)];
    buttonqq.layer.cornerRadius = 32;
    buttonqq.clipsToBounds = YES;
    [buttonqq setBackgroundImage:[UIImage imageNamed:@"qqicon.png"] forState:UIControlStateNormal];
    [buttonqq addTarget:self action:@selector(buttonQq:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonEmail = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 1.45, self.view.frame.size.height *3.05 / 4, 64, 64)];
    buttonEmail.layer.cornerRadius = 32;
    buttonEmail.clipsToBounds = YES;
    [buttonEmail setBackgroundImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
    [buttonEmail addTarget:self action:@selector(buttonEmail:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonSina];
    [self.view addSubview:buttonqq];
    [self.view addSubview:buttonEmail];
    [labelTitle release];
    [labelDetil release];
    [_imageView release];
    [buttonBack release];
    [buttonSina release];
    [buttonqq release];
    [buttonEmail release];
}


//动态图
- (void) appear : (NSTimer *) timer
{
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 3, _scroll.frame.size.height / 3, 5, 5)];
       // (110, 130, 5, 5)];
        image.image = [UIImage imageNamed:@"earth.png"];
    [_scroll addSubview:image];

    [UIImageView beginAnimations:@"第一个" context:nil];
    [UIImageView setAnimationDuration:0.7];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image.frame = CGRectMake(_scroll.frame.size.width / 3 - 10, _scroll.frame.size.height / 3 - 10, 23, 23);//变大会右移和下移, 改变坐标(-5, -5)
    
    [UIImageView commitAnimations];
    
       [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(appear2:) userInfo:nil repeats:NO];// 2
    
    [image release];
}

- (void) appear2 : (NSTimer *) timer
{
    UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 4, self.view.frame.size.height / 3, 5, 5)];
    
    image2.image = [UIImage imageNamed:@"heart.png"];
    
    [UIImageView beginAnimations:@"第二个" context:nil];
    [UIImageView setAnimationDuration:0.7];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image2.frame = CGRectMake(_scroll.frame.size.width / 4 - 10, self.view.frame.size.height / 3 - 10, 23, 23);
    
    [UIImageView commitAnimations];
    
    [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(appear3:) userInfo:nil repeats:NO]; //3

    [_scroll addSubview:image2];
    [image2 release];
}

- (void) appear3 : (NSTimer *) timer
{
    UIImageView *image3 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width * 2 / 3 + 5, _scroll.frame.size.height / 2, 5, 5)];
    
    image3.image = [UIImage imageNamed:@"mp3.png"];
    
    [UIImageView beginAnimations:@"第三个" context:nil];
    [UIImageView setAnimationDuration:0.7];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image3.frame = CGRectMake(_scroll.frame.size.width * 2 / 3 - 10, _scroll.frame.size.height / 2 - 10, 23, 23);
    
    [UIImageView commitAnimations];

     [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(appear4:) userInfo:nil repeats:NO]; //4
    
    [_scroll addSubview:image3];
    [image3 release];
}

- (void) appear4 : (NSTimer *) timer
{
    UIImageView *image4 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width *2.7 / 4, self.view.frame.size.height / 4, 5, 5)];
    
    image4.image = [UIImage imageNamed:@"pic.png"];
    
    [UIImageView beginAnimations:@"第四个" context:nil];
    [UIImageView setAnimationDuration:0.4];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image4.frame = CGRectMake(_scroll.frame.size.width *2.7 / 4, self.view.frame.size.height / 4, 14, 14);
    
    [UIImageView commitAnimations];

    [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(appear5:) userInfo:nil repeats:NO]; //5
    
    [_scroll addSubview:image4];
    [image4 release];
}

- (void) appear5 : (NSTimer *) timer
{
    UIImageView *image5 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 2.8, _scroll.frame.size.height / 1.8, 5, 5)];
    
    image5.image = [UIImage imageNamed:@"dangerous.png"];
    
    [UIImageView beginAnimations:@"第五个" context:nil];
    [UIImageView setAnimationDuration:0.4];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image5.frame = CGRectMake(_scroll.frame.size.width / 2.8 - 10, _scroll.frame.size.height / 1.8 - 10, 14, 14);
    
    [UIImageView commitAnimations];

    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(appear6:) userInfo:nil repeats:NO]; //6
    
    [_scroll addSubview:image5];
    [image5 release];
}

- (void) appear6 : (NSTimer *) timer
{
    UIImageView *image6 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 2 , _scroll.frame.size.height / 4, 5, 5)];
    
    image6.image = [UIImage imageNamed:@"weixin.png"];
    [UIImageView beginAnimations:@"第六个" context:nil];
    [UIImageView setAnimationDuration:0.4];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image6.frame = CGRectMake(_scroll.frame.size.width / 2 - 10, _scroll.frame.size.height / 4 - 10, 14, 14);
    
    [UIImageView commitAnimations];

    
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(appear7:) userInfo:nil repeats:NO]; //7
    
    [_scroll addSubview:image6];
    [image6 release];
}

- (void) appear7 : (NSTimer *) timer
{
    UIImageView *image7 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width *1.8 / 3, _scroll.frame.size.height / 1.7, 5, 5)];
    
    image7.image = [UIImage imageNamed:@"small.png"];
    [UIImageView beginAnimations:@"第七个" context:nil];
    [UIImageView setAnimationDuration:0.2];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image7.frame = CGRectMake(_scroll.frame.size.width *1.8 / 3 - 10, _scroll.frame.size.height / 1.7 - 10, 14, 14);
    
    [UIImageView commitAnimations];

    
    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(appear8:) userInfo:nil repeats:NO]; //8
    
    [_scroll addSubview:image7];
    [image7 release];
}

- (void) appear8 : (NSTimer *) timer
{
    UIImageView *image8 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 1.7, _scroll.frame.size.height / 3.4, 5, 5)];
    
    image8.image = [UIImage imageNamed:@"sina.png"];
    
    [UIImageView beginAnimations:@"第八个" context:nil];
    [UIImageView setAnimationDuration:0.3];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image8.frame = CGRectMake(_scroll.frame.size.width / 1.7 - 10, _scroll.frame.size.height / 3.4 - 10, 14, 14);
    
    [UIImageView commitAnimations];

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(appear9:) userInfo:nil repeats:NO]; //9
    
    [_scroll addSubview:image8];
    [image8 release];
}

- (void) appear9 : (NSTimer *) timer
{
    UIImageView *image9 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width *3.8 / 5, self.view.frame.size.height / 3, 5, 5)];
    
    image9.image = [UIImage imageNamed:@"message.png"];
    [UIImageView beginAnimations:@"第九个" context:nil];
    [UIImageView setAnimationDuration:0.6];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image9.frame = CGRectMake(_scroll.frame.size.width *3.8 / 5 - 10, self.view.frame.size.height / 3 - 10, 23, 23);
    
    [UIImageView commitAnimations];

    [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(appear10:) userInfo:nil repeats:NO]; //10
    
    [_scroll addSubview:image9];
    [image9 release];
}

- (void) appear10 : (NSTimer *) timer
{
    UIImageView *image10 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width *3.3 / 5, _scroll.frame.size.height / 5, 5, 5)];
    image10.image = [UIImage imageNamed:@"camera.png"];
    [UIImageView beginAnimations:@"第十个" context:nil];
    [UIImageView setAnimationDuration:0.7];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image10.frame = CGRectMake(_scroll.frame.size.width *3.3 / 5 - 10, _scroll.frame.size.height / 5 - 10, 23, 23);
    
    [UIImageView commitAnimations];

    
    [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(appear11:) userInfo:nil repeats:NO]; //10

    [_scroll addSubview:image10];
    [image10 release];
}

- (void) appear11 : (NSTimer *) timer
{
    UIImageView *image11 = [[UIImageView alloc] initWithFrame:CGRectMake(_scroll.frame.size.width / 2.3, self.view.frame.size.height / 2.2, 5, 5)];
    image11.image = [UIImage imageNamed:@"foot.png"];
    [UIImageView beginAnimations:@"第十一个" context:nil];
    [UIImageView setAnimationDuration:0.7];
    [UIImageView setAnimationRepeatAutoreverses:NO];
    [UIImageView setAnimationRepeatCount:NAN];
    image11.frame = CGRectMake(_scroll.frame.size.width / 2.3 - 10, self.view.frame.size.height / 2.2 - 10, 23, 23);
    
    [UIImageView commitAnimations];

    //自动滚动的动画

    [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(appear12:) userInfo:nil repeats:NO]; //11

    [_scroll addSubview:image11];
    [image11 release];
}

- (void) appear12 : (NSTimer *) timer
{
    [UIScrollView beginAnimations:@"13213" context:nil];
    [UIScrollView setAnimationDuration:1];
    [UIScrollView setAnimationRepeatCount:NAN];
    [UIScrollView setAnimationRepeatAutoreverses:NO];
    _scroll.contentOffset = CGPointMake(_scroll.frame.size.width, 0);
    self.page.currentPage = _scroll.contentOffset.x / self.scroll.frame.size.width;
    [UIScrollView commitAnimations];

}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage = scrollView.contentOffset.x / self.scroll.frame.size.width;
}


//第三方点击事件
//新浪登陆
- (void) buttonSina : (UIButton *) button
{
   
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        //        NSLog(@"%d",result);
        if (result) {
                        NSString *uid = [userInfo uid];
                        NSString *nickname = [userInfo nickname];
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            [user1 setObject:uid forKey:@"userSina"];
            
            NSLog(@"%@", uid);
            NSLog(@"%@", nickname);
                        //NSString *profileImage = [userInfo profileImage];
                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登陆成功" message:[NSString stringWithFormat:@"授权登陆成功,用户ID:%@,昵称:%@",uid,nickname] delegate:nil cancelButtonTitle:@"我要浏览!" otherButtonTitles:nil];
                        [alert show];
            
            UITabBarController *tabBar = (UITabBarController *)[[UIApplication sharedApplication].delegate window].rootViewController;
            
            tabBar.selectedIndex = 0;
            [self dismissViewControllerAnimated:YES completion:nil];
            [alert release];
            
            //存入登录状态
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:@"yes" forKey:@"login"];
            Singleton *share = [Singleton shareInstance];
            share.loginState = SinaLogin;
            [user setValue:@"sina" forKey:@"state"];
        }
    }];
    
}

- (void) buttonQq : (UIButton *) button
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"不好意思" message:@"目前不支持QQ登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}

- (void) buttonEmail : (UIButton *) button
{
    LocalViewController *local = [[LocalViewController alloc] init];
   
    [self.navigationController pushViewController:local animated:YES];
    [local release];
}

- (void)back
{
    //获取tabBar的数组, 并且进行页面逻辑
//    UITabBarController *tabBar = (UITabBarController *)[[UIApplication sharedApplication].delegate window].rootViewController;
//    
//    tabBar.selectedIndex = 0;
    
    [self dismissViewControllerAnimated:YES completion:nil];

}


@end
